/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alee.managers.glasspane;

import com.alee.utils.SwingUtils;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * User: mgarin Date: 05.10.11 Time: 18:03
 * <p/>
 * This manager provides you with single instances of WebGlassPane for each window available in your application when needed
 */

public class GlassPaneManager
{
    // Registered windows
    private static Map<Window, WebGlassPane> registeredWindows = new HashMap<Window, WebGlassPane> ();

    /**
     * Finds or creates WebGlassPane for window and shows it
     */

    public static WebGlassPane getGlassPane ( Component component )
    {
        return getGlassPane ( SwingUtils.getWindowAncestor ( component ) );
    }

    public static WebGlassPane getGlassPane ( Window window )
    {
        if ( window != null )
        {
            if ( registeredWindows.containsKey ( window ) )
            {
                return registeredWindows.get ( window );
            }
            else
            {
                if ( window instanceof JFrame )
                {
                    registerWindow ( ( JFrame ) window );
                }
                else if ( window instanceof JDialog )
                {
                    registerWindow ( ( JDialog ) window );
                }
                else if ( window instanceof JWindow )
                {
                    registerWindow ( ( JWindow ) window );
                }
                else
                {
                    return null;
                }
                return getGlassPane ( window );
            }
        }
        else
        {
            return null;
        }
    }

    /**
     * Creates WebGlassPane for window and shows it
     */

    public static void registerWindow ( JFrame window )
    {
        if ( !registeredWindows.containsKey ( window ) )
        {
            WebGlassPane glassPane = createGlassPane ( window );
            window.setGlassPane ( glassPane );
            glassPane.setVisible ( true );
            window.invalidate ();
            registeredWindows.put ( window, glassPane );
        }
    }

    public static void registerWindow ( JDialog window )
    {
        if ( !registeredWindows.containsKey ( window ) )
        {
            WebGlassPane glassPane = createGlassPane ( window );
            window.setGlassPane ( glassPane );
            glassPane.setVisible ( true );
            window.invalidate ();
            registeredWindows.put ( window, glassPane );
        }
    }

    public static void registerWindow ( JWindow window )
    {
        if ( !registeredWindows.containsKey ( window ) )
        {
            WebGlassPane glassPane = createGlassPane ( window );
            window.setGlassPane ( glassPane );
            glassPane.setVisible ( true );
            window.invalidate ();
            registeredWindows.put ( window, glassPane );
        }
    }

    /**
     * Creates WebGlassPane for single window
     */

    private static WebGlassPane createGlassPane ( Window window )
    {
        return new WebGlassPane ( window );
    }
}
